/**
 * 
 */
package cn.itcast.core.controller;

import java.io.Serializable;
import java.io.StringWriter;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.common.web.session.SessionProvider;
import cn.itcast.core.bean.BuyCart;
import cn.itcast.core.bean.BuyItem;
import cn.itcast.core.bean.product.Sku;
import cn.itcast.core.bean.user.Addr;
import cn.itcast.core.bean.user.Buyer;
import cn.itcast.core.query.user.AddrQuery;
import cn.itcast.core.service.product.SkuService;
import cn.itcast.core.service.user.AddrService;
import cn.itcast.core.web.Constants;

/**
 * 购物车
 * 
 * @author 赵新国
 *
 */
@Controller
public class CartController {

	//购买按钮
	@RequestMapping(value = "/shopping/buyCart.shtml")
	public String buyCart(Integer skuId, Integer amount, Integer buyLimit, Integer productId,
			HttpServletResponse response, HttpServletRequest request, ModelMap model) {
			
			ObjectMapper om = new ObjectMapper();
			
			om.setSerializationInclusion(Inclusion.NON_NULL);
			
			//声明
			BuyCart buyCart = null;
			
			//判断Cookie是否有购物车
			//JESSIONID
			//buyCart_cookie
			//......
			Cookie[] cookies = request.getCookies();
			if(null != cookies && cookies.length > 0){
				for(Cookie c : cookies){
					if(Constants.BUYCART_COOKIE.equals(c.getName())){
						//如果有了，就使用该购物车
						String value = c.getValue();
						try {
							buyCart = om.readValue(value, BuyCart.class);
						} catch (Exception e) {
							e.printStackTrace();
						}
						break;
					}
				}
			}
			
			//如果没有，则创建购物车
			
			if(null == buyCart){
				//第三步：购物车
				buyCart = new BuyCart();
			}
			//第一步:Sku
			if(null != skuId){
				Sku sku = new Sku();
				sku.setId(skuId);
				
				//限制
				if(null != buyLimit){
					sku.setSkuUpperLimit(buyLimit);
				}
				
				//第二步:创建购物项
				BuyItem buyItem = new BuyItem();
				buyItem.setSku(sku);
				//数量 1 2 3 -1
				buyItem.setAmount(amount);
				
				//创建List的话，这样每次都是最后一款
				/*List<BuyItem> items = new ArrayList<BuyItem>();*/
				
				//添加购物项
				buyCart.addItem(buyItem);
				
				//添加最后一款商品的id
				if(null != productId){
					buyCart.setProductId(productId);
				}
				
				//输出流
				StringWriter str = new StringWriter();
				
				//对象转JSON 写的过程 Json是字符串流
				try {
					om.writeValue(str, buyCart);
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				//第四步：把购物车放到cookie中 	购物车对象转json
				Cookie cookie = new Cookie(Constants.BUYCART_COOKIE,str.toString());
				//关闭浏览器	也要有Cookie
				//默认是-1	表示关闭浏览器Cookie就没有了
				//销毁是0		表示关闭浏览器Cookie马上就没有了
				//expiry	设置时间，单位是秒
				cookie.setMaxAge(60*60*24);//1天
				//路径
				// /shopping/buyCart.shtml
				//默认	/shopping
				//	/buyer/*.shtml
				cookie.setPath("/");
				//发送
				response.addCookie(cookie);
				
			}
			//将购物车装满
			List<BuyItem> items = buyCart.getItems();
			for(BuyItem item : items){
				Sku s = skuService.getSkuByKey(item.getSku().getId());
				item.setSku(s);
				//小计
			}
			
			//小计
			
			
			model.addAttribute("buyCart", buyCart);
		
			return "product/cart";
	}
	
	
	//清空购物车
	@RequestMapping(value = "/shopping/clearCart.shtml")
	public String clearCart(HttpServletRequest request, HttpServletResponse response){
		
		Cookie cookie = new Cookie(Constants.BUYCART_COOKIE, null);
		cookie.setMaxAge(0);
		cookie.setPath("/");
		response.addCookie(cookie);
		
		return "redirect:/shopping/buyCart.shtml";
	}
	
	//删除一个购物项
	@RequestMapping(value = "/shopping/deleteItem.shtml")
	public String deleteItem(HttpServletRequest request,HttpServletResponse response,Integer skuId){
		
		ObjectMapper om = new ObjectMapper();
		
		om.setSerializationInclusion(Inclusion.NON_NULL);
		
		//声明
		BuyCart buyCart = null;
		
		//判断Cookie是否有购物车
		//JESSIONID
		//buyCart_cookie
		//......
		Cookie[] cookies = request.getCookies();
		if(null != cookies && cookies.length > 0){
			for(Cookie c : cookies){
				if(Constants.BUYCART_COOKIE.equals(c.getName())){
					//如果有了，就使用该购物车
					String value = c.getValue();
					try {
						buyCart = om.readValue(value, BuyCart.class);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				}
			}
		}
		
		if(null != buyCart){
			Sku sku = new Sku();
			sku.setId(skuId);
			
			//第二步:创建购物项
			BuyItem buyItem = new BuyItem();
			buyItem.setSku(sku);
			
			buyCart.deleteItem(buyItem);
			
			//把cookie写回去
			//输出流
			StringWriter str = new StringWriter();
			
			//对象转JSON 写的过程 Json是字符串流
			try {
				om.writeValue(str, buyCart);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			//第四步：把购物车放到cookie中 	购物车对象转json
			Cookie cookie = new Cookie(Constants.BUYCART_COOKIE,str.toString());
			//关闭浏览器	也要有Cookie
			//默认是-1	表示关闭浏览器Cookie就没有了
			//销毁是0		表示关闭浏览器Cookie马上就没有了
			//expiry	设置时间，单位是秒
			cookie.setMaxAge(60*60*24);//1天
			//路径
			// /shopping/buyCart.shtml
			//默认	/shopping
			//	/buyer/*.shtml
			cookie.setPath("/");
			//发送
			response.addCookie(cookie);
		}
		
		return "redirect:/shopping/buyCart.shtml";
	}
	
	//结算
	@RequestMapping(value = "/buyer/trueBuy.shtml")
	public String trueBuy(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		ObjectMapper om = new ObjectMapper();
		om.setSerializationInclusion(Inclusion.NON_NULL);
		//声明
		BuyCart buyCart = null;
		//判断Cookie是否有购物车
		//JESSIONID
		//buyCart_cookie
		//......
		Cookie[] cookies = request.getCookies();
		if(null != cookies && cookies.length > 0){
			for(Cookie c : cookies){
				if(Constants.BUYCART_COOKIE.equals(c.getName())){
					//如果有了，就使用该购物车
					String value = c.getValue();
					try {
						buyCart = om.readValue(value, BuyCart.class);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				}
			}
		}
		//判断购物车中是否有商品
		if(null != buyCart){
			//判断购物车中的商品是否还有库存
			List<BuyItem> items = buyCart.getItems();
			if(null != items && items.size() > 0){
				//购物车中的商品项
				Integer i = items.size();
				
				//判断购物车中的商品是否还有库存
				for(BuyItem it : items){
					Sku sku = skuService.getSkuByKey(it.getSku().getId());
					//判断库存<个数
					if(sku.getStockInventory() < it.getAmount()){
						//清理购物车，删除次商品
						buyCart.deleteItem(it);
					}
				}
				//清理后商品项个数 l=0
				Integer l = items.size();
				//判断清理前后
				if(i > l){
					//修改Cookie中的购物车数据
					
					//把cookie写回去
					//输出流
					StringWriter str = new StringWriter();
					
					//对象转JSON 写的过程 Json是字符串流
					try {
						om.writeValue(str, buyCart);
					} catch (Exception e) {
						e.printStackTrace();
					}
					
					//第四步：把购物车放到cookie中 	购物车对象转json
					Cookie cookie = new Cookie(Constants.BUYCART_COOKIE,str.toString());
					//关闭浏览器	也要有Cookie
					//默认是-1	表示关闭浏览器Cookie就没有了
					//销毁是0		表示关闭浏览器Cookie马上就没有了
					//expiry	设置时间，单位是秒
					cookie.setMaxAge(60*60*24);//1天
					//路径
					// /shopping/buyCart.shtml
					//默认	/shopping
					//	/buyer/*.shtml
					cookie.setPath("/");
					//发送
					response.addCookie(cookie);
					
					return "redirect:/shopping/buyCart.shtml";
				}else{
					//收货地址加载
					Buyer buyer = (Buyer) sessionProvider.getAttribute(request, Constants.BUYER_SESSION);
					AddrQuery addrQuery = new AddrQuery();
					addrQuery.setBuyerId(buyer.getUsername());
					//默认是1
					addrQuery.setIsDef(1);
					List<Addr> addrs = addrService.getAddrList(addrQuery);
					model.addAttribute("addr", addrs.get(0));
					
					//将购物车装满
					List<BuyItem> its = buyCart.getItems();
					for(BuyItem item : its){
						Sku s = skuService.getSkuByKey(item.getSku().getId());
						item.setSku(s);
						//小计
					}
					
					//小计
					
					
					model.addAttribute("buyCart", buyCart);
					
					//正常跳转
					return "product/productOrder";
				}
			}else{
				return "redirect:/shopping/buyCart.shtml";
			}
		}else{
			return "redirect:/shopping/buyCart.shtml";
		}
		//正常跳转
		//return "product/productOrder";
	}
	
	@Autowired
	private SkuService skuService;
	
	@Autowired
	private AddrService addrService;
	
	@Autowired
	private SessionProvider sessionProvider;
	
	
	
}
